app.controller('adminController', ['$rootScope', '$scope', '$parse', '$http', 'userServices', 'eventServices', 'taskServices', function($rootScope, $scope, $parse, $http, userServices, eventServices, taskServices)
{
	$rootScope.currentUser = userServices.getLoggedUser();
	
	console.info('Initialize all services');
	
	/**
	* Get personal events
	*/ 
	$scope.getPersonalEventsCommand = function() {
		try {
			
			if($rootScope.currentUser == '' || $rootScope.currentUser == undefined) {
				throw 'Utilisateur non authentifié';
			}
		
			$scope.personalEventLoading = true;
			eventServices.personnalEvents (function (data) {
				console.debug('userServices authenticate user received');
				if(data.isFailed) {
					toastr.warning(data.exception, "Authentification");
				}
				else {
					$scope.personalevents = data.response;
				}
				$scope.personalEventLoading = false;
			}, $rootScope.currentUser.Id);
		}
		catch(exception) {
			toastr.error(exception, "Authentification");
		}
	};
	
	/**
	* Set current user information on UI
	*/ 
	$scope.setPersonalInformationsCommand = function() {
		try {
		
			$scope.loadingPersonalInformations = true;

			if($rootScope.currentUser == '' || $rootScope.currentUser == undefined) {
				throw 'Utilisateur non authentifié';
			}
			
			$scope.personalUserId = $rootScope.currentUser.Id;
			$scope.personalUserFirstname = $rootScope.currentUser.Firstname;
			$scope.personalUserLastname = $rootScope.currentUser.Lastname;
			$scope.personalUserEmail = $rootScope.currentUser.Email;
			$scope.personalUserPhoneNumber = $rootScope.currentUser.PhoneNumber;
			$scope.personalUserWebAddress = $rootScope.currentUser.WebAddress;
			$scope.personalUserLogin = $rootScope.currentUser.Login;
			
			$scope.personalUserHasAvatar = ($rootScope.currentUser.Avatar == '') ? false : true;
			if($scope.personalUserHasAvatar == true) {
				console.debug('user has avatar');
				$scope.personalUserAvatar = $rootScope.currentUser.Avatar;
				$scope.personalUserAvatarMime = $rootScope.currentUser.AvatarMime;
			}

			$scope.loadingPersonalInformations = false;
		}
		catch(exception) {
			toastr.error(exception, "Personnal information");
		}
	};
	
	/**
	*	Load the admin component
	*/
	$scope.loadAdminComponentCommand = function() 
	{
		console.info("loadAdminComponentCommand - called");
		
		console.info("Set variables");
		$scope.getPersonalEventsCommand();
		$scope.setPersonalInformationsCommand();
	};
	
	$scope.loadAdminComponentCommand();
}]);